package com.gpc.hexapods.layer;

import android.opengl.GLES20;

import com.gpc.hexapods.data.BodyData;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class BodyLayer {

    private BodyData mBodyData;
    private int mPositionHandle;
    private int mColorHandle;

    private FloatBuffer mPositionBuffer;
    private FloatBuffer mColorBuffer;

    public BodyLayer() {
        mBodyData = new BodyData();

        mPositionBuffer = ByteBuffer.allocateDirect(mBodyData.position.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        mPositionBuffer.put(mBodyData.position);
        mPositionBuffer.position(0);

        mColorBuffer = ByteBuffer.allocateDirect(mBodyData.color.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        mColorBuffer.put(mBodyData.color);
        mColorBuffer.position(0);
    }

    public void setHandle(int positionHandle, int colorHandle) {
        mPositionHandle = positionHandle;
        mColorHandle = colorHandle;
    }

    public void onDraw() {
        GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0, mPositionBuffer);
        GLES20.glVertexAttribPointer(mColorHandle, 4, GLES20.GL_FLOAT, false, 0, mColorBuffer);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, mBodyData.position.length / 3);
    }
}
